{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[this doc on github](https://github.com/dotnet/interactive/tree/main/samples/notebooks/powershell)\n",
    "\n",
    "# Working with Azure PowerShell <img src=\"https://raw.githubusercontent.com/PowerShell/PowerShell/master/assets/Powershell_black_64.png\" align=\"right\"/>\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "You'll need to install a few Az modules to use this Notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "Install-Module Az.Compute,Az.Resources,Az.KeyVault -Force"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First connect to your Azure account."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "Connect-AzAccount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If your account contains more than one active subscription the first one will be selected for further use. To select another subscription, use Set-AzContext."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "$myAzSubscription = \"My Subscription\"\n",
    "Set-AzContext -Subscription $myAzSubscription"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup common variables\n",
    "\n",
    "These variables are used throughout the notebook so set them at the top so they can be used everywhere.\n",
    "\n",
    "> NOTE: This also means that all you have to do is change the vaules here and the Notebook will just work still (so long as the values are correct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "# Used all over\n",
    "$RESOURCE_GROUP_NAME = 'JupyterTest'\n",
    "$LOCATION = 'East US 2'\n",
    "\n",
    "# Resource names\n",
    "$VAULT_NAME = 'myAzVault'\n",
    "$VM_NAME = 'myAzVM'\n",
    "\n",
    "# Single instances\n",
    "$VM_USERNAME = 'azureuser'\n",
    "$VM_IMAGE = 'UbuntuLTS'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a resource group with the `New-AzResourceGroup` command\n",
    "\n",
    "An Azure resource group is a logical container into which Azure resources are deployed and managed. A resource group must be created before a virtual machine. In the following example, a resource group named myResourceGroupVM is created in the EastUS region:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "New-AzResourceGroup -ResourceGroupName $RESOURCE_GROUP_NAME -Location $LOCATION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a KeyVault in Azure\n",
    "\n",
    "We will use this to store the password to our VM for future use."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "New-AzKeyVault -Name $VAULT_NAME -Location $LOCATION -ResourceGroupName $RESOURCE_GROUP_NAME"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a new Azure VM\n",
    "\n",
    "### Generate a secret that will be used for the password\n",
    "\n",
    "> Note: You should switch this to key-based authentication or something else in the future,\n",
    "> but this is fine for the purposes of this demo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "$secret = [System.IO.Path]::GetRandomFileName() | ConvertTo-SecureString -AsPlainText"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Store the secret in KeyVault for future usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "Set-AzKeyVaultSecret -VaultName $VAULT_NAME -SecretValue $secret -Name VMpassword"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate a credential object for the `New-AzVM` command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "$cred = [pscredential]::new($VM_USERNAME, $secret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create our VM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "$splat = @{\n",
    "    Image = $VM_IMAGE\n",
    "    Credential = $cred\n",
    "    ResourceGroupName = $RESOURCE_GROUP_NAME\n",
    "    Location = $LOCATION\n",
    "    Name = $VM_NAME\n",
    "}\n",
    "\n",
    "New-AzVM @splat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point you should be able to run `Get-AzVM` and your VM should show up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "Get-AzVM -Name $VM_NAME -Status"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (PowerShell)",
   "language": "PowerShell",
   "name": ".net-powershell"
  },
  "language_info": {
   "file_extension": ".ps1",
   "mimetype": "text/x-powershell",
   "name": "PowerShell",
   "pygments_lexer": "powershell",
   "version": "7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}